package com.longdaji.decoration.ui.activitiesOfMine.receivingAddress;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.AddressBean;
import com.longdaji.decoration.utils.AddressPickerDialog;
import com.longdaji.decoration.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

public class ReceivingAddressActivity extends BaseActivity implements ReceivingAddressContract.View {

    @Inject
    ReceivingAddressContract.Presenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.et_receiving_people)
    EditText etReceivingPeople;
    @BindView(R.id.et_receiving_phone)
    EditText etReceivingPhone;
    @BindView(R.id.tv_receiving_area)
    TextView tvReceivingArea;
    @BindView(R.id.tv_receiving_campus)
    TextView tvReceivingCampus;
    @BindView(R.id.et_domitory)
    EditText etDomitory;
    @BindView(R.id.cb_set_default)
    CheckBox cbSetDefault;
    @BindView(R.id.et_detail_receiving_address)
    EditText etDetailReceivingAddress;
    @BindView(R.id.rl_set_default)
    RelativeLayout mRlytDefault;

    private AddressPickerDialog mAreaPickerDialog;
    private AddressPickerDialog mCampusPickerDialog;

    private boolean isDefaultAddress = false;

    private Account account;
    private String receivingProvince, receivingCity, receivingDistrict, receivingStreet;
    private AddressBean.Address address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiving_address);

        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        initView();
    }

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, ReceivingAddressActivity.class);
        return intent;
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            address = intent.getParcelableExtra("addr_bean");
            if (address != null) {
                tvTitle.setText("更新收货地址");
                etReceivingPeople.setText(address.getContactPerson());
                etReceivingPhone.setText(address.getContactTelephone());

                tvReceivingArea.setText(address.getSimpleAddress());
                etDetailReceivingAddress.setText(address.getDetailAddress());
                if (TextUtils.equals("1", address.getDefaultAddress())) {
                    mRlytDefault.setVisibility(View.GONE);
                    cbSetDefault.setChecked(true);
                } else {
                    mRlytDefault.setVisibility(View.VISIBLE);
                    cbSetDefault.setChecked(false);
                }

            } else {
                tvTitle.setText("添加收货地址");
            }

        }
        account = Account.getInstance();
        // TODO: 2018/4/2 0002 有些地区有street，有些没有
        mPresenter.initCampusPicker();
        mAreaPickerDialog = new AddressPickerDialog(this);
        mCampusPickerDialog = new AddressPickerDialog(this);
        mAreaPickerDialog.selector.setOnAddressSelectedListener(new OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(Province province, City city, County county, Street street) {
                mAreaPickerDialog.dismiss();
                receivingProvince = province.name;
                receivingCity = city.name;
                if (county != null) {
                    receivingDistrict = county.name;
                } else {
                    receivingDistrict = "";
                }
                if (street != null) {
                    receivingStreet = street.name;
                } else {
                    receivingStreet = "";
                }
                tvReceivingArea.setText(receivingProvince + receivingCity + receivingDistrict + receivingStreet);

            }
        });


        mCampusPickerDialog.selector.setAddressProvider(new AddressProvider() {
            @Override
            public void provideProvinces(AddressReceiver<Province> addressReceiver) {
                addressReceiver.send(mPresenter.getProvince());
            }

            @Override
            public void provideCitiesWith(int provinceId, AddressReceiver<City> addressReceiver) {
                List<City> cityList = new ArrayList<>();
                for (City city : mPresenter.getCity()) {
                    if (city.province_id == provinceId) {
                        cityList.add(city);
                    }
                }
                addressReceiver.send(cityList);
            }

            @Override
            public void provideCountiesWith(int cityId, AddressReceiver<County> addressReceiver) {
                List<County> countyList = new ArrayList<>();
                for (County county : mPresenter.getCampus()) {
                    if (county.city_id == cityId) {
                        countyList.add(county);
                    }
                }
                addressReceiver.send(countyList);
            }

            @Override
            public void provideStreetsWith(int i, AddressReceiver<Street> addressReceiver) {
                addressReceiver.send(null);
            }
        });

        mCampusPickerDialog.selector.setOnAddressSelectedListener(new OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(Province province, City city, County county, Street street) {
                mCampusPickerDialog.dismiss();
                tvReceivingCampus.setText(county.name);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back, R.id.tv_finish, R.id.rl_receiving_area,
            R.id.rl_receiving_street, R.id.rl_receiving_campus, R.id.rl_set_default
    })
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.tv_finish:
                // TODO: 2018/4/6 0006 defaultAddress 这里是应该是boolean还是"是","否",shippingId快递单号？填什么
                checkInfo();

                break;
            case R.id.rl_receiving_area:
                mAreaPickerDialog.show();
                break;
            case R.id.rl_receiving_street:
                break;
            case R.id.rl_receiving_campus:
                mCampusPickerDialog.show();
                break;
            default:
                break;
        }
    }

    private void checkInfo() {
        if (TextUtils.isEmpty(etReceivingPeople.getText()) || TextUtils.isEmpty(etReceivingPeople.getText().toString().trim())) {
            toast("请填写收货人");
            return;
        }
        if (TextUtils.isEmpty(etReceivingPhone.getText()) || TextUtils.isEmpty(etReceivingPhone.getText().toString().trim())) {
            toast("请填写联系电话");
            return;
        }
        if (TextUtils.isEmpty(tvReceivingArea.getText()) || TextUtils.isEmpty(etReceivingPeople.getText().toString().trim())) {
            toast("请填写所在地区");
            return;
        }
        if (TextUtils.isEmpty(etDetailReceivingAddress.getText()) || TextUtils.isEmpty(etDetailReceivingAddress.getText().toString().trim())) {
            toast("请填写详细地址");
            return;
        }
        if (!CommonUtil.isMobile(etReceivingPhone.getText().toString())) {
            toast("请填写11位电话号码");
            return;
        }
        String defaultAddress;
        if (cbSetDefault.isChecked()) {
            defaultAddress = "1";
        } else {
            defaultAddress = "0";
        }
        if (address != null) {
            mPresenter.onFinishClick(account.getUserid(), etReceivingPeople.getText().toString(), etReceivingPhone.getText().toString(),
                    receivingProvince, receivingCity, receivingDistrict, receivingStreet, etDetailReceivingAddress.getText().toString(),
                    null, defaultAddress, address.getShippingId(), null, null

            );
        } else {
            mPresenter.onFinishClick(account.getUserid(), etReceivingPeople.getText().toString(), etReceivingPhone.getText().toString(),
                    receivingProvince, receivingCity, receivingDistrict, receivingStreet, etDetailReceivingAddress.getText().toString(),
                    null, defaultAddress, -1, null, null);
        }

    }


    @Override
    public void showResult(boolean isSucceed) {
        if (isSucceed) {
            toast("添加收货地址成功");
            Log.d("tmptest", "add succeed");
//            finish();
        } else {
            toast("添加收货地址失败");
        }
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void showUpdateAddrSuccess() {
        toast("更新收货地址成功");
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void showUpdateAddrFail() {
        toast("更新收货地址失败");
        setResult(RESULT_OK);
        finish();
    }
}
